package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.DeviceCollect;
import org.dromara.system.domain.bo.DeviceCollectBo;
import org.dromara.system.domain.vo.DeviceCollectListVo;
import org.dromara.system.domain.vo.DeviceVo;
import org.dromara.system.mapper.DeviceCollectMapper;
import org.dromara.system.mapper.DeviceMapper;
import org.dromara.system.service.IDeviceCollectService;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * 设备收藏Service业务层处理
 *
 * @author Lion Li
 * @date 2024-10-21
 */
@RequiredArgsConstructor
@Service
public class DeviceCollectServiceImpl implements IDeviceCollectService {

    private final DeviceCollectMapper baseMapper;

    private final DeviceMapper deviceMapper;


    /**
     * 分页查询设备收藏列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 设备收藏分页列表
     */
//    @Override
//    public TableDataInfo<DeviceCollectListVo> queryPageList(DeviceCollectBo bo, PageQuery pageQuery) {
//        Long userId = LoginHelper.getUserId();
//        Page<DeviceCollectListVo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
//        Page<DeviceCollectListVo> result = baseMapper.queryPageList(page,bo.getGroupId(),bo.getDeviceName(),userId);
//        System.out.println(result);
//        return TableDataInfo.build(result);
//    }


    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DeviceCollect entity) {
        //TODO 做一些数据校验,如唯一约束
    }


    @Override
    public int deviceCollect(Collection<Long> ids) {
        Long userId = LoginHelper.getUserId();
        if (ids.isEmpty()) {
            return baseMapper.deleteDeviceCollect(userId);
        } else {
            baseMapper.deleteDeviceCollect(userId);
            return baseMapper.deviceCollect(userId, ids);
        }
//        return baseMapper.deviceCollects(ids);

    }

    @Override
    public int deviceCollectById(Long id) {
        Long userId = LoginHelper.getUserId();
        Long count = baseMapper.selectCount(Wrappers.<DeviceCollect>lambdaQuery().eq(DeviceCollect::getUserId, userId).eq(DeviceCollect::getDeviceId, id));
        if (count == 0) {
            return baseMapper.deviceCollectById(userId, id);
//            DeviceVo deviceVo = deviceMapper.selectVoById(id);
        } else {
            return baseMapper.deleteDeviceCollectById(userId, id);
        }
//        DeviceVo deviceVo = deviceMapper.selectVoById(id);
//        if (deviceVo.getCollect()==0){
//            return baseMapper.deviceCollectById(id);
//        }else {
//            return baseMapper.deleteDeviceCollectById(id);
//        }
    }
}
